import CryptoJS from './crypto-js.js'

class AESUtil{
	
	config = {
		debug:false,
		key:"d7b85c6e414dbcda",
		iv:"d7b85c6e414dbcda"
	}
	setConfig(config) {
		this.config = Object.assign(this.config, config)
		if (this.config.debug) {
			console.log(`set config data: `, this.config);
		}
	}
	/**
	 * 解密方法
	 * @param {Object} t
	 */
	decrypt(t) {
		let key = CryptoJS.enc.Utf8.parse(this.config.key);
		let iv = CryptoJS.enc.Utf8.parse(this.config.iv);
		let encryptedHexStr = CryptoJS.enc.Hex.parse(t);
		let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
		let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
		let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
		return decryptedStr.toString();
	}
	/**
	 * 加密方法
	 * @param {Object} t
	 */
	encrypt(t) {
		let key = CryptoJS.enc.Utf8.parse(this.config.key);
		let iv = CryptoJS.enc.Utf8.parse(this.config.iv);
		let srcs = CryptoJS.enc.Utf8.parse(t);
		let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
		return encrypted.ciphertext.toString().toUpperCase();
	}
}
	
const AesUtil = new AESUtil();
export default AesUtil